package Demo01;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28463
 * Date: 2022—06—13
 * Time: 15:22
 */
//写一个泛型类，找数组中的最大值
//只要这个T，实现了这个接口就可以
class Alg<T extends Comparable<T>> {
    //泛型类
    public T findMaxVal(T[] array) {
        T maxVal = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i].compareTo(maxVal) > 0) {
                maxVal = array[i];
            }
        }
        return maxVal;
    }
}
class Alg2 {
    //静态方法
    public static<T extends Comparable<T>> T findMaxVal(T[] array) {
            T maxVal = array[0];
            for (int i = 1; i < array.length; i++) {
                if (array[i].compareTo(maxVal) > 0) {
                    maxVal = array[i];
                }
            }
            return maxVal;
    }
}
class Alg3 {
    //泛型方法：成员方法
    public <T extends Comparable<T>> T findMaxVal2(T[] array) {
        T maxVal = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i].compareTo(maxVal) > 0) {
                maxVal = array[i];
            }
        }
        return maxVal;
    }
}
public class Test02 {
    public static void main(String[] args) {
        Alg3 alg3 = new Alg3();
        Integer[] array = {100,200,24,34,55};
        int val = alg3.findMaxVal2(array);
        System.out.println(val);
    }
    public static void main2(String[] args) {
        Integer[] array = {100,200,24,34,55};
        //int val = Alg2.<Integer>findMaxVal(array);
        //要调用这个方法，就必须要有个对象，如果不想有这个对象，那就可以把这个方法变成静态的就可以了
        int val = Alg2.findMaxVal(array);
        System.out.println(val);
    }
    public static void main1(String[] args) {
        Alg<Integer> alg = new Alg<>();
        Integer[] array = {100,200,24,34,55};
        int val = alg.findMaxVal(array);
        System.out.println(val);
    }
}
